package com.ficus.road.maintain.core.interceptor.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author jinxin.tang
 * @date 2021/1/21
 */
@Aspect
@Component
@Slf4j
public class LogExecutionTimeAspectImpl {

    @Around("@annotation(com.ficus.road.maintain.core.interceptor.annotation.LogExecutionTime)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        try {
            Object result = joinPoint.proceed();
            log.debug("Time Aspect: '{}', cost time {} ms", joinPoint.getSignature(), System.currentTimeMillis() - startTime);
            return result;
        } catch (Throwable t) {
            log.debug("Exception happened: '{}', cost time {} ms", joinPoint.getSignature(), System.currentTimeMillis() - startTime);
            throw t;
        }
    }
}
